/*
 * File:   Matrix2D.h
 * Author: jcrada
 *
 * Created on 3 June 2012, 12:39 AM
 */

#ifndef ECB_MATRIX2D_H
#define	ECB_MATRIX2D_H

#include <vector>
#include <sstream>
#include <string>

namespace ecb {

    template <class T>
    class Matrix2D {
    protected:
        std::vector<std::vector<T> > _matrix;
        int _numberOfRows, _numberOfCols;
    public:

        const std::vector<T>& operator[](int row) {
            return _matrix[row];
        }

        Matrix2D() : _numberOfRows(0), _numberOfCols(0) { }

        Matrix2D(int size, T defaultValue = T()) :
        _matrix(std::vector<std::vector<T> >(size, std::vector<T > (size, defaultValue))),
        _numberOfRows(size), _numberOfCols(size) { }

        Matrix2D(int rows, int cols, T defaultValue = T()) :
        _matrix(std::vector<std::vector<T> >(rows, std::vector<T > (cols, defaultValue))),
        _numberOfRows(rows), _numberOfCols(cols) { }

        virtual void setDimensions(int rows, int cols, T defaultValue = T()) {
            _numberOfRows = rows;
            _numberOfCols = cols;
            _matrix = std::vector<std::vector<T> >(rows, std::vector<T > (cols, defaultValue));
        }

        virtual int getNumberOfRows() const {
            return _numberOfRows;
        }

        virtual int getNumberOfCols() const {
            return _numberOfCols;
        }

        virtual void set(int row, int col, T value) {
            _matrix[row][col] = value;
        }

        virtual T get(int row, int col) {
            return _matrix[row][col];
        }

        virtual std::string toString() const {
            std::stringstream ss;

            for (int row = 0; row < getNumberOfRows(); ++row) {
                ss << "[ ";
                for (int col = 0; col < getNumberOfCols(); ++col) {
                    ss << _matrix[row][col] << " ";
                }
                ss << "]\n";
            }

            return ss.str();
        }


    };

}

#endif	/* ECB_MATRIX2D_H */

